﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlaktDataReader
{
    class MoveOutBookTextFile : TextFileBase
    {
        public List<MoveOutBookRecord> Records { get; set; }

        public MoveOutBookTextFile()
        {
            _fields.Add("Lopnr", -1);
            _fields.Add("Sidnr", -1);
            _fields.Add("Ordnr", -1);
            _fields.Add("Datum", -1);
            _fields.Add("Kon", -1);
            _fields.Add("Attnr", -1);
            _fields.Add("Titel", -1);
            _fields.Add("Fnamn", -1);
            _fields.Add("Enamn", -1);
            _fields.Add("Fradress", -1);
            _fields.Add("Nfradress", -1);
            _fields.Add("Tilladress", -1);
            _fields.Add("Ntilladress", -1);
            _fields.Add("Fdatum", -1);
            _fields.Add("Fadress", -1);
            _fields.Add("Nfadress", -1);
            _fields.Add("Ovr1", -1);
            _fields.Add("Ovr2", -1);
            _fields.Add("Ovr3", -1);
            _fields.Add("Mtitel", -1);
            _fields.Add("Mfnamn", -1);
            _fields.Add("Menamn", -1);
            _fields.Add("Ktitel", -1);
            _fields.Add("Kfnamn", -1);
            _fields.Add("Kenamn", -1);
            _fields.Add("Uppdaterat", -1);

            Separator = '\t';
            Records = new List<MoveOutBookRecord>();
            FileEncoding = Encoding.UTF7;
        }

        private MoveOutBook bk;

        protected override void _AddRecord(string[] RowFields)
        {
            if (RowFields.Length > 0)
            {
                MoveOutBookRecord r = new MoveOutBookRecord();
                for (int i = 0; i < RowFields.Length; i++)
                {
                    if (_fields.ContainsValue(i))
                    {
                        try
                        {
                            switch (_fields.Where(f => f.Value == i).First().Key)
                            {
                                case "Lopnr":
                                    try
                                    {
                                        r.RowID = Convert.ToInt32(RowFields[i]);
                                    }
                                    catch
                                    {
                                        r.RowID = 0;
                                    }
                                    break;
                                case "Sidnr":
                                    r.PageNo = RowFields[i];
                                    break;
                                case "Ordnr":
                                    r.OrdNo = RowFields[i];
                                    break;
                                case "Datum":
                                    r.MoveOutDate = RowFields[i];
                                    break;
                                case "Kon":
                                    try
                                    {
                                        if (!string.IsNullOrWhiteSpace(RowFields[i]))
                                        {
                                            r.Gender = Convert.ToChar(RowFields[i]);
                                        }
                                        else
                                        {
                                            r.Gender = 'O';
                                        }
                                    }
                                    catch
                                    {
                                        r.Gender = 'O';
                                    }
                                    break;
                                case "Attnr":
                                    r.AttestNo = RowFields[i];
                                    break;
                                case "Titel":
                                    r.Title = RowFields[i];
                                    break;
                                case "Fnamn":
                                    r.FirstName = RowFields[i];
                                    break;
                                case "Enamn":
                                    r.SurName = RowFields[i];
                                    break;
                                case "Fradress":
                                    r.FRAddress = RowFields[i];
                                    break;
                                case "Nfradress":
                                    r.NFRAddress = RowFields[i];
                                    break;
                                case "Tilladress":
                                    r.ToAdress = RowFields[i];
                                    break;
                                case "Ntilladress":
                                    r.NToAddress = RowFields[i];
                                    break;
                                case "Fdatum":
                                    r.BornDate = RowFields[i];
                                    break;
                                case "Fadress":
                                    r.BornAddress = RowFields[i];
                                    break;
                                case "Nfadress":
                                    r.NBornAddress = RowFields[i];
                                    break;
                                case "Ovr1":
                                    r.Extra1 = RowFields[i];
                                    break;
                                case "Ovr2":
                                    r.Extra2 = RowFields[i];
                                    break;
                                case "Ovr3":
                                    r.Extra3 = RowFields[i];
                                    break;
                                case "Mtitel":
                                    r.MaleTitle = RowFields[i];
                                    break;
                                case "Mfnamn":
                                    r.MaleFirstName = RowFields[i];
                                    break;
                                case "Menamn":
                                    r.MaleSurName = RowFields[i];
                                    break;
                                case "Ktitel":
                                    r.FemaleTitle = RowFields[i];
                                    break;
                                case "Kfnamn":
                                    r.FemaleFirstName = RowFields[i];
                                    break;
                                case "Kenamn":
                                    r.FemaleSurName = RowFields[i];
                                    break;
                                case "Uppdaterat":
                                    r.Updated = RowFields[i];
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex) { /*System.Windows.MessageBox.Show(ex.Message); */ }
                    }
                }
                if (r.IsChanged)
                {
                    r.IsChanged = false;
                    Records.Add(r);
                }
            }
        }
    }
}
